home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gxcpath.c < prev    next >
C/C++ Source or Header  |  1993-05-26  |  24KB  |  743 lines

  1. /* Copyright (C) 1991, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxcpath.c */
  20. /* Implementation of clipping paths */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gxdevice.h"
  24. #include "gxfixed.h"
  25. #include "gzcolor.h"
  26. #include "gzpath.h"
  27. #include "gxcpath.h"
  28.  
  29. const uint gs_clip_path_sizeof = sizeof(gx_clip_path);
  30.  
  31. /* Imported procedures */
  32. gx_device *gs_currentdevice(P1(gs_state *));
  33. void gx_set_device_only(P2(gs_state *, gx_device *));
  34.  
  35. /* Forward references */
  36. private void clip_prepare(P1(gx_clip_list *));
  37. private void gx_clip_list_init(P1(gx_clip_list *));
  38. private void gx_clip_list_from_rectangle(P2(gx_clip_list *, gs_fixed_rect *));
  39. private int gx_clip_list_add_to_path(P2(gx_clip_list *, gx_path *));
  40. private void gx_clip_list_free(P2(gx_clip_list *, const gs_memory_procs *));
  41.  
  42. private const gx_clip_list clip_list_empty =
  43. {  { 0, 0, min_int, min_int, min_int, min_int },
  44.    { 0, 0, min_int, max_int, 0, 0 },
  45.    { 0, 0, max_int, max_int, max_int, max_int },
  46.    0
  47. };
  48.  
  49. /* Debugging */
  50.  
  51. #define clip_rect_print(str, ar)\
  52.   if_debug6('q', "[q]%s %lx: (%d,%d),(%d,%d)\n", str, (ulong)ar,\
  53.         (ar)->xmin, (ar)->ymin, (ar)->xmax, (ar)->ymax)
  54.  
  55. #ifdef DEBUG
  56. /* Validate a clipping path that has gone through clip_prepare. */
  57. private void
  58. clip_list_validate(gx_clip_list *clp)
  59. {    gx_clip_rect *prev = &clp->first;
  60.     gx_clip_rect *ptr = prev;
  61.     int wrong = 0;
  62.     while ( ptr != 0 )
  63.       { if ( ptr->ymin > ptr->ymax || ptr->xmin > ptr->xmax ||
  64.         !(ptr->ymin >= prev->ymax ||
  65.           ptr->ymin == prev->ymin && ptr->ymax == prev->ymax &&
  66.           ptr->xmin >= prev->xmax)
  67.         )
  68.           { clip_rect_print("WRONG:", ptr);
  69.         wrong = 1;
  70.           }
  71.         prev = ptr, ptr = ptr->next;
  72.       }
  73. }
  74. #endif
  75.  
  76. /* ------ Clipping path accessing ------ */
  77.  
  78. /* Initialize a clipping path. */
  79. int
  80. gx_cpath_init(gx_clip_path *pcpath, const gs_memory_procs *mp)
  81. {    static /*const*/ gs_fixed_rect null_rect = { { 0, 0 }, { 0, 0 } };
  82.     gx_path_init(&pcpath->path, mp);
  83.     return gx_cpath_from_rectangle(pcpath, &null_rect, mp);
  84. }
  85.  
  86. /* Return the path of a clipping path. */
  87. int
  88. gx_cpath_path(gx_clip_path *pcpath, gx_path *ppath)
  89. {    if ( !pcpath->segments_valid )
  90.        {    int code = gx_clip_list_add_to_path(&pcpath->list, &pcpath->path);
  91.         if ( code < 0 ) return code;
  92.         pcpath->segments_valid = 1;
  93.        }
  94.     *ppath = pcpath->path;
  95.     return 0;
  96. }
  97.  
  98. /* Return the quick-check rectangle for a clipping path. */
  99. int
  100. gx_cpath_box_for_check(const gx_clip_path *pcpath, gs_fixed_rect *pbox)
  101. {    *pbox = pcpath->cbox;
  102.     return 0;
  103. }
  104.  
  105. /* Test if a clipping path includes a rectangle. */
  106. /* The rectangle need not be oriented correctly, i.e. x0 > x1 is OK. */
  107. int
  108. gx_cpath_includes_rectangle(register const gx_clip_path *pcpath,
  109.   fixed x0, fixed y0, fixed x1, fixed y1)
  110. {    return
  111.         (x0 <= x1 ?
  112.             (pcpath->cbox.p.x <= x0 && x1 <= pcpath->cbox.q.x) :
  113.             (pcpath->cbox.p.x <= x1 && x0 <= pcpath->cbox.q.x)) &&
  114.         (y0 <= y1 ?
  115.             (pcpath->cbox.p.y <= y0 && y1 <= pcpath->cbox.q.y) :
  116.             (pcpath->cbox.p.y <= y1 && y0 <= pcpath->cbox.q.y));
  117. }
  118.  
  119. /* Release a clipping path. */
  120. void
  121. gx_cpath_release(gx_clip_path *pcpath)
  122. {    if ( !pcpath->shares_list )
  123.         gx_clip_list_free(&pcpath->list, pcpath->path.memory_procs);
  124.     gx_path_release(&pcpath->path);
  125. }
  126.  
  127. /* Share a clipping path. */
  128. void
  129. gx_cpath_share(gx_clip_path *pcpath)
  130. {    gx_path_share(&pcpath->path);
  131.     pcpath->shares_list = 1;
  132. }
  133.  
  134. /* Create a rectangular clipping path. */
  135. /* The supplied rectangle may not be oriented correctly, */
  136. /* but it will be oriented correctly upon return. */
  137. int
  138. gx_cpath_from_rectangle(gx_clip_path *pcpath, gs_fixed_rect *pbox, const gs_memory_procs *mp)
  139. {    gx_clip_list_from_rectangle(&pcpath->list, pbox);
  140.     pcpath->cbox = *pbox;
  141.     pcpath->segments_valid = 0;
  142.     pcpath->shares_list = 0;
  143.     gx_path_init(&pcpath->path, mp);
  144.     pcpath->path.bbox = *pbox;
  145.     return 0;
  146. }
  147.  
  148. /* Intersect a new clipping path with an old one. */
  149. /* Note that it may overwrite its path argument; return 1 in this case, */
  150. /* otherwise 0 for success, <0 for failure as usual. */
  151. int
  152. gx_cpath_intersect(gs_state *pgs, gx_clip_path *pcpath, gx_path *ppath, int rule)
  153. {    gs_fixed_rect old_box, new_box;
  154.     int code;
  155.     if ( gx_cpath_is_rectangle(pcpath, &old_box) &&
  156.         gx_path_is_rectangle(ppath, &new_box)
  157.        )
  158.        {    int changed = 0;
  159.         /* Intersect the two rectangles if necessary. */
  160.         if ( old_box.p.x > new_box.p.x )
  161.             new_box.p.x = old_box.p.x, changed = 1;
  162.         if ( old_box.p.y > new_box.p.y )
  163.             new_box.p.y = old_box.p.y, changed = 1;
  164.         if ( old_box.q.x < new_box.q.x )
  165.             new_box.q.x = old_box.q.x, changed = 1;
  166.         if ( old_box.q.y < new_box.q.y )
  167.             new_box.q.y = old_box.q.y, changed = 1;
  168.         if ( changed )
  169.            {    /* Store the new rectangle back into the new path. */
  170.             register segment *pseg =
  171.                 (segment *)ppath->first_subpath;
  172. #define set_pt(pqx,pqy)\
  173.   pseg->pt.x = new_box.pqx.x, pseg->pt.y = new_box.pqy.y
  174.             set_pt(p, p); pseg = pseg->next;
  175.             set_pt(q, p); pseg = pseg->next;
  176.             set_pt(q, q); pseg = pseg->next;
  177.             set_pt(p, q); pseg = pseg->next;
  178.             if ( pseg != 0 ) /* might be an open rectangle */
  179.               set_pt(p, p);
  180. #undef set_pt
  181.            }
  182.         ppath->bbox = new_box;
  183.         gx_clip_list_from_rectangle(&pcpath->list, &new_box);
  184.         pcpath->cbox = new_box;
  185.         pcpath->path = *ppath;
  186.         pcpath->segments_valid = 1;
  187.         code = 1;
  188.        }
  189.     else
  190.        {    /* Not a rectangle.  Intersect the slow way. */
  191.         gx_device_accum adev;
  192.         gx_device_color devc;
  193.         gx_device *save_dev = gs_currentdevice(pgs);
  194.         adev = gs_accum_device;
  195.         adev.memory_procs = pcpath->path.memory_procs;
  196.         (*adev.procs->open_device)((gx_device *)&adev);
  197.         devc.color1 = devc.color2 = 0;    /* arbitrary, but not */
  198.                     /* transparent */
  199.         devc.halftone_level = 0;
  200.         gx_set_device_only(pgs, (gx_device *)&adev);
  201.         code = gx_fill_path(ppath, &devc, pgs, rule, fixed_half);
  202.         gx_set_device_only(pgs, save_dev);
  203.         if ( code < 0 ||
  204.              (code = (*adev.procs->close_device)((gx_device *)&adev)) < 0
  205.            )
  206.         {    gx_clip_list_free(&adev.list, adev.memory_procs);
  207.             return code;
  208.         }
  209.         pcpath->list = adev.list;
  210.         gx_path_init(&pcpath->path, pcpath->path.memory_procs);
  211.         pcpath->path.bbox.p.x = int2fixed(adev.bbox.p.x);
  212.         pcpath->path.bbox.p.y = int2fixed(adev.bbox.p.y);
  213.         pcpath->path.bbox.q.x = int2fixed(adev.bbox.q.x);
  214.         pcpath->path.bbox.q.y = int2fixed(adev.bbox.q.y);
  215.         /* Note that the result of the intersection might be */
  216.         /* a single rectangle.  This will cause clip_path_is_rect.. */
  217.         /* to return true.  This, in turn, requires that */
  218.         /* we set pcpath->cbox correctly. */
  219.         if ( clip_list_is_rectangle(&adev.list) )
  220.             pcpath->cbox = pcpath->path.bbox;
  221.         else
  222.            {    /* The quick check must fail. */
  223.             pcpath->cbox.p.x = pcpath->cbox.p.y = 0;
  224.             pcpath->cbox.q.x = pcpath->cbox.q.y = 0;
  225.            }
  226.         pcpath->segments_valid = 0;
  227.         pcpath->shares_list = 0;
  228.         code = 0;
  229.        }
  230.     return code;
  231. }
  232.  
  233. /* ------ Clipping list routines ------ */
  234.  
  235. /* Initialize a clip list. */
  236. private void
  237. gx_clip_list_init(gx_clip_list *clp)
  238. {    *clp = clip_list_empty;
  239. }
  240.  
  241. /* Initialize a clip list to a rectangle. */
  242. /* The supplied rectangle may not be oriented correctly, */
  243. /* but it will be oriented correctly upon return. */
  244. private void
  245. gx_clip_list_from_rectangle(gx_clip_list *clp, register gs_fixed_rect *rp)
  246. {    gx_clip_list_init(clp);
  247.     if ( rp->p.x > rp->q.x )
  248.       { fixed t = rp->p.x; rp->p.x = rp->q.x; rp->q.x = t; }
  249.     if ( rp->p.y > rp->q.y )
  250.       { fixed t = rp->p.y; rp->p.y = rp->q.y; rp->q.y = t; }
  251.     clp->sole.xmin = fixed2int_var(rp->p.x);
  252.     clp->sole.ymin = fixed2int_var(rp->p.y);
  253.     clp->sole.xmax = fixed2int_var_ceiling(rp->q.x);
  254.     clp->sole.ymax = fixed2int_var_ceiling(rp->q.y);
  255.     clp->count = 1;
  256. }
  257.  
  258. /* Add a clip list to a path. */
  259. /* The current implementation is very inefficient. */
  260. private int
  261. gx_clip_list_add_to_path(gx_clip_list *clp, gx_path *ppath)
  262. {    gx_clip_rect *rp;
  263.     int code;
  264.     clip_prepare(clp);
  265.     for ( rp = &clp->first; rp != 0; rp = rp->next )
  266.        {    if ( rp->xmin < rp->xmax && rp->ymin < rp->ymax )
  267.            {    code = gx_path_add_rectangle(ppath,
  268.                     int2fixed(rp->xmin),
  269.                     int2fixed(rp->ymin),
  270.                     int2fixed(rp->xmax),
  271.                     int2fixed(rp->ymax));
  272.             if ( code < 0 ) return code;
  273.            }
  274.        }
  275.     return 0;
  276. }
  277.  
  278. /* Free a clip list. */
  279. private void
  280. gx_clip_list_free(gx_clip_list *clp, const gs_memory_procs *mp)
  281. {    gx_clip_rect *rp = clp->last.prev;
  282.     if ( clp->count <= 1 ) return;
  283.     clip_prepare(clp);
  284.     while ( rp != &clp->first )
  285.        {    gx_clip_rect *prev = rp->prev;
  286.         (*mp->free)((char *)rp, 1, sizeof(gx_clip_rect), "gx_clip_list_free");
  287.         rp = prev;
  288.        }
  289.     gx_clip_list_init(clp);
  290. }
  291.  
  292. /* Prepare a clip list for enumeration, */
  293. /* by splicing pointers to account for possible relocation. */
  294. private void
  295. clip_prepare(register gx_clip_list *clp)
  296. {    if ( clp->count <= 1 )
  297.        {    clp->first.next = clp->last.prev = &clp->sole;
  298.         clp->sole.prev = &clp->first;
  299.         clp->sole.next = &clp->last;
  300.        }
  301.     else
  302.        {    clp->first.next->prev = &clp->first;
  303.         clp->last.prev->next = &clp->last;
  304.        }
  305. }
  306.  
  307. /* ------ Rectangle list accumulator ------ */
  308.  
  309. /* Device for accumulating a clipping region. */
  310. private dev_proc_open_device(accum_open);
  311. private dev_proc_close_device(accum_close);
  312. private dev_proc_fill_rectangle(accum_fill_rectangle);
  313.  
  314. /* The device descriptor */
  315. /* Many of these procedures won't be called; they are set to NULL. */
  316. private gx_device_procs accum_procs = {
  317.     accum_open,
  318.     NULL,                /* get_initial_matrix */
  319.     NULL,                /* sync_output */
  320.     NULL,                /* output_page */
  321.     accum_close,
  322.     NULL,                /* map_rgb_color */
  323.     NULL,                /* map_color_rgb */
  324.     accum_fill_rectangle
  325. };
  326. const gx_device_accum gs_accum_device =
  327. {    sizeof(gx_device_accum),
  328.     &accum_procs,
  329.     "clip list accumulator",
  330.     0, 0, 1, 1, no_margins, dci_black_and_white, 0    /* generic */
  331. };
  332. #define adev ((gx_device_accum *)dev)
  333.  
  334. /* Initialize the accumulation device. */
  335. private int
  336. accum_open(register gx_device *dev)
  337. {    gx_clip_list_init(&adev->list);
  338.     adev->last = &adev->list.first;
  339.     adev->bbox.p.x = adev->bbox.p.y = max_int;
  340.     adev->bbox.q.x = adev->bbox.q.y = min_int;
  341.     return 0;
  342. }
  343.  
  344. /* Close the accumulation device. */
  345. private int
  346. accum_close(gx_device *dev)
  347. {    if ( adev->list.count >= 2 )
  348.        {    /* 'sole' isn't good for much of anything, */
  349.         /* and it complicates the bookkeeping.... */
  350.         gx_clip_rect *last = adev->last;
  351.         gx_clip_rect *ar =
  352.           (gx_clip_rect *)(*adev->memory_procs->alloc)(1, sizeof(gx_clip_rect), "accum_close");
  353.         if ( ar == 0 ) return_error(gs_error_VMerror);
  354.         *ar = adev->list.sole;
  355.         adev->list.sole.prev->next = ar;
  356.         if ( last == &adev->list.sole )
  357.             last = ar;
  358.         else
  359.             adev->list.sole.next->prev = ar;
  360.         adev->list.last.prev = last;
  361.        }
  362. #ifdef DEBUG
  363. if ( gs_debug['q'] )
  364.    {    gx_clip_rect *rp = &adev->list.first;
  365.     adev->last->next = 0;
  366.     while ( rp != 0 )
  367.        {    clip_rect_print("   ", rp);
  368.         rp = rp->next;
  369.        }
  370.    }
  371.     clip_prepare(&adev->list); /* just for clip_list_validate */
  372.     clip_list_validate(&adev->list);
  373. #endif
  374.     return 0;
  375. }
  376.  
  377. /* Accumulate one rectangle. */
  378. #define accum_alloc(s, ar, px, py, qx, qy)\
  379.    {    ar = (adev->list.count == 0 ? &adev->list.sole :\
  380.        (gx_clip_rect *)(*adev->memory_procs->alloc)(1, sizeof(gx_clip_rect), "accum_rect"));\
  381.     if ( ar == 0 ) return_error(gs_error_VMerror);\
  382.     ar->xmin = px, ar->ymin = py, ar->xmax = qx, ar->ymax = qy;\
  383.     adev->list.count++;\
  384.     clip_rect_print(s, ar);\
  385.    }
  386. #define accum_add_last(ar)\
  387.     adev->last->next = ar, ar->prev = adev->last, adev->last = ar
  388. #define accum_add_after(ar, rprev)\
  389.     ar->prev = rprev, ar->next = rprev->next;\
  390.     if ( rprev != adev->last ) rprev->next->prev = ar;\
  391.     else adev->last = ar;\
  392.     rprev->next = ar
  393. #define accum_add_before(ar, rnext)\
  394.     ar->prev = rnext->prev, ar->next = rnext,\
  395.       rnext->prev->next = ar, rnext->prev = ar
  396. /* Add a rectangle to the list.  It would be wonderful if rectangles */
  397. /* were always presented in the correct order, but they aren't, */
  398. /* because the fill loop works by trapezoids, not by scan lines. */
  399. /* All we can count on is that they are disjoint and *approximately* */
  400. /* in order. */
  401. #undef adev
  402. private int
  403. accum_add_rect(gx_device_accum *adev, int x, int y, int xe, int ye)
  404. {    gx_clip_rect *nr, *ar, *rptr;
  405.     int ymin, ymax;
  406. top:    rptr = adev->last;
  407.     accum_alloc("accum", nr, x, y, xe, ye);
  408.     if ( y >= rptr->ymax ||
  409.         y == rptr->ymin && ye == rptr->ymax && x >= rptr->xmax
  410.        )
  411.        {    accum_add_last(nr);
  412.         return 0;
  413.        }
  414.     /* Work backwards till we find the insertion point. */
  415.     while ( ye <= rptr->ymin ) rptr = rptr->prev;
  416.     ymin = rptr->ymin;
  417.     ymax = rptr->ymax;
  418.     if ( ye > ymax )
  419.        {    if ( y >= ymax )
  420.            {    /* Insert between two bands. */
  421.             accum_add_after(nr, rptr);
  422.             return 0;
  423.            }
  424.         /* Split off the top part of the new rectangle. */
  425.         accum_alloc("a.top", ar, x, ymax, xe, ye);
  426.         accum_add_after(ar, rptr);
  427.         ye = nr->ymax = ymax;
  428.         clip_rect_print(" ymax", nr);
  429.        }
  430.     /* Here we know ymin < ye <= ymax; */
  431.     /* rptr points to the last node with this value of ymin/ymax. */
  432.     /* Split the existing band if necessary. */
  433.     if ( ye < ymax )
  434.        {    gx_clip_rect *rsplit = rptr;
  435.         while ( rsplit->ymax == ymax )
  436.            {    accum_alloc("s.top", ar, rsplit->xmin, ye, rsplit->xmax, ymax);
  437.             accum_add_after(ar, rptr);
  438.             rsplit->ymax = ye;
  439.             rsplit = rsplit->prev;
  440.            }
  441.         ymax = ye;
  442.        }
  443.     if ( y > ymin )
  444.        {    gx_clip_rect *rbot = rptr, *rsplit;
  445.         while ( rbot->prev->ymin == ymin )
  446.             rbot = rbot->prev;
  447.         for ( rsplit = rbot; ; )
  448.            {    accum_alloc("s.bot", ar, rsplit->xmin, ymin, rsplit->xmax, y);
  449.             accum_add_before(ar, rbot);
  450.             rsplit->ymin = y;
  451.             if ( rsplit == rptr ) break;
  452.             rsplit = rsplit->next;
  453.            }
  454.         ymin = y;
  455.        }
  456.     /* Search for the X insertion point. */
  457.     /* The new rectangle is guaranteed disjoint from all the old ones. */
  458.     while ( rptr->ymin == ymin && x < rptr->xmax )
  459.        {    rptr = rptr->prev;
  460.        }
  461.     if ( y < ymin )
  462.        {    /* Continue with the bottom part of the new rectangle. */
  463.         nr->ymin = ymin;
  464.         clip_rect_print(" ymin", nr);
  465.         accum_add_after(nr, rptr);
  466.         ye = ymin;
  467.         goto top;
  468.        }
  469.     accum_add_after(nr, rptr);
  470.     return 0;
  471. }
  472. #define adev ((gx_device_accum *)dev)
  473. private int
  474. accum_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  475.   gx_color_index color)
  476. {    int xe, ye;
  477.     if ( w <= 0 || h <= 0 ) return 0;
  478.     xe = x + w, ye = y + h;
  479.     /* Update the bounding box. */
  480.     if ( x < adev->bbox.p.x ) adev->bbox.p.x = x;
  481.     if ( y < adev->bbox.p.y ) adev->bbox.p.y = y;
  482.     if ( xe > adev->bbox.q.x ) adev->bbox.q.x = xe;
  483.     if ( ye > adev->bbox.q.y ) adev->bbox.q.y = ye;
  484.     return accum_add_rect(adev, x, y, xe, ye);
  485. }
  486.  
  487. /* ------ Rectangle list clipper ------ */
  488.  
  489. /* Device for clipping with a region. */
  490. private dev_proc_open_device(clip_open);
  491. private dev_proc_get_initial_matrix(clip_get_initial_matrix);
  492. private dev_proc_map_rgb_color(clip_map_rgb_color);
  493. private dev_proc_map_color_rgb(clip_map_color_rgb);
  494. private dev_proc_fill_rectangle(clip_fill_rectangle);
  495. private dev_proc_tile_rectangle(clip_tile_rectangle);
  496. private dev_proc_copy_mono(clip_copy_mono);
  497. private dev_proc_copy_color(clip_copy_color);
  498. private dev_proc_get_bits(clip_get_bits);
  499. private dev_proc_get_props(clip_get_props);
  500. private dev_proc_put_props(clip_put_props);
  501. private dev_proc_map_cmyk_color(clip_map_cmyk_color);
  502. private dev_proc_get_xfont_procs(clip_get_xfont_procs);
  503. private dev_proc_get_xfont_device(clip_get_xfont_device);
  504.  
  505. /* The device descriptor. */
  506. private gx_device_procs clip_procs = {
  507.     clip_open,
  508.     clip_get_initial_matrix,
  509.     gx_default_sync_output,
  510.     gx_default_output_page,
  511.     gx_default_close_device,
  512.     clip_map_rgb_color,
  513.     clip_map_color_rgb,
  514.     clip_fill_rectangle,
  515.     clip_tile_rectangle,
  516.     clip_copy_mono,
  517.     clip_copy_color,
  518.     gx_default_draw_line,
  519.     clip_get_bits,
  520.     clip_get_props,
  521.     clip_put_props,
  522.     clip_map_cmyk_color,
  523.     clip_get_xfont_procs,
  524.     clip_get_xfont_device
  525. };
  526. const gx_device_clip gs_clip_device =
  527. {    sizeof(gx_device_clip),
  528.     &clip_procs,
  529.     "clipper",
  530.     0, 0, 1, 1, no_margins, dci_black_and_white, 0    /* generic */
  531. };
  532. #define rdev ((gx_device_clip *)dev)
  533.  
  534. /* Declare and initialize the cursor variables. */
  535. #ifdef DEBUG
  536. private ulong clip_in, clip_down, clip_down2, clip_up, clip_x, clip_no_x;
  537. #  define inc(v) v++
  538. #else
  539. #  define inc(v) 0
  540. #endif
  541. #define DECLARE_CLIP\
  542.   register gx_clip_rect *rptr = rdev->current.rptr;\
  543.   gx_device *tdev = rdev->target;
  544. /* Check whether the rectangle x,y,w,h falls within the current entry. */
  545. #define xywh_in_ryptr()\
  546.   ((y >= rptr->ymin && y + h <= rptr->ymax &&\
  547.     x >= rptr->xmin && x + w <= rptr->xmax) ? (inc(clip_in), 1) : 0)
  548. /*
  549.  * Warp the cursor forward or backward to the first rectangle row that
  550.  * could include a given y value.  Assumes rptr is set, and updates it.
  551.  * Specifically, after warp_cursor, y < rptr->ymax and y >= rptr->prev->ymax.
  552.  * Note that ye <= rptr->ymin is possible.
  553.  */
  554. #define warp_cursor(y)\
  555.   while ( (y) >= rptr->ymax ) { inc(clip_up); rptr = rptr->next; };\
  556.   while ( rptr->prev != 0 && (y) < rptr->prev->ymax )\
  557.    { inc(clip_down); rptr = rptr->prev; }
  558. /*
  559.  * Enumerate the rectangles of the x,w,y,h argument that fall within
  560.  * the clipping region.  Usage:
  561.  *    BEGIN_CLIP
  562.  *        (adjust for yc > y if necessary)
  563.  *    FOR_CLIP
  564.  *        ... xc, yc, xec, yec ... [must be an expression statement]
  565.  *    NEXT_CLIP
  566.  *        (about to set yc to yec)
  567.  *    END_CLIP
  568.  */
  569. #define BEGIN_CLIP\
  570.     if ( w <= 0 || h <= 0 ) return 0;\
  571.    {    int yc;\
  572.     const int xe = x + w, ye = y + h;\
  573.     warp_cursor(y);\
  574.     rdev->current.rptr = rptr;\
  575.     yc = rptr->ymin;\
  576.     if ( yc < y ) yc = y;\
  577.     else if ( yc >= ye ) return 0;
  578. #define FOR_CLIP\
  579.     for ( ; ; )\
  580.        {    const int ymax = rptr->ymax;\
  581.         int yec = ymax;\
  582.         if ( yec > ye ) yec = ye;\
  583.         if_debug2('q', "[q]yc=%d yec=%d\n", yc, yec);\
  584.         do \
  585.            {    int xc = rptr->xmin;\
  586.             int xec = rptr->xmax;\
  587.             if ( xc < x ) xc = x;\
  588.             if ( xec > xe ) xec = xe;\
  589.             if ( xec > xc )\
  590.                {    int code;\
  591.                 clip_rect_print("match", rptr);\
  592.                 if_debug2('q', "[q]xc=%d xec=%d\n", xc, xec);\
  593.                 inc(clip_x);\
  594.                 code =
  595. #define NEXT_CLIP\
  596.                 if ( code < 0 ) return code;\
  597.                }\
  598.             else inc(clip_no_x);\
  599.            }\
  600.         while ( (rptr = rptr->next) != 0 && rptr->ymax == ymax );\
  601.         if ( rptr == 0 || (yec = rptr->ymin) >= ye ) break;
  602. #define END_CLIP\
  603.         yc = yec;\
  604.        }\
  605.    }
  606.  
  607. /* Open a clipping device */
  608. private int
  609. clip_open(register gx_device *dev)
  610. {    gx_device *tdev = rdev->target;
  611.     /* Fix up possible dangling pointers. */
  612.     clip_prepare(&rdev->list);
  613.     /* Initialize the cursor. */
  614.     rdev->current.rptr = &rdev->list.first;
  615.     rdev->color_info = tdev->color_info;
  616.     return 0;
  617. }
  618.  
  619. /* Forward non-displaying operations to the target device. */
  620. private void
  621. clip_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  622. {    gx_device *tdev = rdev->target;
  623.     (*tdev->procs->get_initial_matrix)(tdev, pmat);
  624. }
  625. private gx_color_index
  626. clip_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  627.   gx_color_value b)
  628. {    gx_device *tdev = rdev->target;
  629.     return (*tdev->procs->map_rgb_color)(tdev, r, g, b);
  630. }
  631. private int
  632. clip_map_color_rgb(gx_device *dev, gx_color_index color,
  633.   gx_color_value prgb[3])
  634. {    gx_device *tdev = rdev->target;
  635.     return (*tdev->procs->map_color_rgb)(tdev, color, prgb);
  636. }
  637. private int
  638. clip_get_props(gx_device *dev, gs_prop_item *plist)
  639. {    gx_device *tdev = rdev->target;
  640.     return (*tdev->procs->get_props)(tdev, plist);
  641. }
  642. private int
  643. clip_put_props(gx_device *dev, gs_prop_item *plist, int count)
  644. {    gx_device *tdev = rdev->target;
  645.     return (*tdev->procs->put_props)(tdev, plist, count);
  646. }
  647. private gx_color_index
  648. clip_map_cmyk_color(gx_device *dev, gx_color_value c, gx_color_value m,
  649.   gx_color_value y, gx_color_value k)
  650. {    gx_device *tdev = rdev->target;
  651.     return (*tdev->procs->map_cmyk_color)(tdev, c, m, y, k);
  652. }
  653. private gx_xfont_procs *
  654. clip_get_xfont_procs(gx_device *dev)
  655. {    gx_device *tdev = rdev->target;
  656.     return (*tdev->procs->get_xfont_procs)(tdev);
  657. }
  658. private gx_device *
  659. clip_get_xfont_device(gx_device *dev)
  660. {    gx_device *tdev = rdev->target;
  661.     return (*tdev->procs->get_xfont_device)(tdev);
  662. }
  663.  
  664. /* Fill a rectangle */
  665. private int
  666. clip_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  667.   gx_color_index color)
  668. {    DECLARE_CLIP
  669.     dev_proc_fill_rectangle((*fill)) = tdev->procs->fill_rectangle;
  670.     if ( xywh_in_ryptr() )
  671.         return (*fill)(tdev, x, y, w, h, color);
  672.     BEGIN_CLIP
  673.     FOR_CLIP
  674.         (*fill)(tdev, xc, yc, xec - xc, yec - yc, color);
  675.     NEXT_CLIP
  676.     END_CLIP
  677.     return 0;
  678. }
  679.  
  680. /* Tile a rectangle */
  681. private int
  682. clip_tile_rectangle(gx_device *dev, const gx_bitmap *tile,
  683.   int x, int y, int w, int h,
  684.   gx_color_index color0, gx_color_index color1, int phase_x, int phase_y)
  685. {    DECLARE_CLIP
  686.     dev_proc_tile_rectangle((*fill)) = tdev->procs->tile_rectangle;
  687.     int code;
  688.     if ( xywh_in_ryptr() )
  689.         return (*fill)(tdev, tile, x, y, w, h, color0, color1, phase_x, phase_y);
  690.     BEGIN_CLIP
  691.     FOR_CLIP
  692.         (*fill)(tdev, tile, xc, yc, xec - xc, yec - yc, color0, color1, phase_x, phase_y);
  693.     NEXT_CLIP
  694.     END_CLIP
  695.     return 0;
  696. }
  697.  
  698. /* Copy a monochrome rectangle */
  699. private int
  700. clip_copy_mono(gx_device *dev,
  701.   const byte *data, int sourcex, int raster, gx_bitmap_id id,
  702.   int x, int y, int w, int h,
  703.   gx_color_index color0, gx_color_index color1)
  704. {    DECLARE_CLIP
  705.     dev_proc_copy_mono((*copy)) = tdev->procs->copy_mono;
  706.     if ( xywh_in_ryptr() )
  707.         return (*copy)(tdev, data, sourcex, raster, id, x, y, w, h, color0, color1);
  708.     BEGIN_CLIP
  709.         if ( yc > y ) data += (yc - y) * raster;
  710.     FOR_CLIP
  711.         (*copy)(tdev, data, sourcex + xc - x, raster, gx_no_bitmap_id, xc, yc, xec - xc, yec - yc, color0, color1);
  712.     NEXT_CLIP
  713.         data += (yec - yc) * raster;
  714.     END_CLIP
  715.     return 0;
  716. }
  717.  
  718. /* Copy a color rectangle */
  719. private int
  720. clip_copy_color(gx_device *dev,
  721.   const byte *data, int sourcex, int raster, gx_bitmap_id id,
  722.   int x, int y, int w, int h)
  723. {    DECLARE_CLIP
  724.     dev_proc_copy_color((*copy)) = tdev->procs->copy_color;
  725.     if ( xywh_in_ryptr() )
  726.         return (*copy)(tdev, data, sourcex, raster, id, x, y, w, h);
  727.     BEGIN_CLIP
  728.         if ( yc > y ) data += (yc - y) * raster;
  729.     FOR_CLIP
  730.         (*copy)(tdev, data, sourcex + xc - x, raster, gx_no_bitmap_id, xc, yc, xec - xc, yec - yc);
  731.     NEXT_CLIP
  732.         data += (yec - yc) * raster;
  733.     END_CLIP
  734.     return 0;
  735. }
  736.  
  737. /* Get bits back from the device. */
  738. private int
  739. clip_get_bits(gx_device *dev, int y, byte *data, byte **actual_data)
  740. {    gx_device *tdev = rdev->target;
  741.     return (*tdev->procs->get_bits)(tdev, y, data, actual_data);
  742. }
  743.